package com.xhj.xmly.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xhj.xmly.R;
import com.xhj.xmly.adapter.DiscoverAdapter;
import com.xhj.xmly.bean.ProvinceList;
import com.xhj.xmly.callback.HttpService;
import com.xhj.xmly.fragment.ProvinceRadioFragment;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ProvinceActivity extends AppCompatActivity implements View.OnClickListener, Callback<ProvinceList> {

    private String url = "http://live.ximalaya.com/";
    private ViewPager viewPager;
    private TabLayout tab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_province);
        viewPager = (ViewPager) findViewById(R.id.prvince_radio_page);
        tab = ((TabLayout) findViewById(R.id.province_radio_tab));
        ImageView imageView_back = (ImageView) findViewById(R.id.btn_image);
        LinearLayout imageView_down = (LinearLayout) findViewById(R.id.province_radio_linear);
        imageView_back.setOnClickListener(this);
        imageView_down.setOnClickListener(this);
        TextView textView = (TextView) findViewById(R.id.radio_name);
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        textView.setText(name);
        HttpService service = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getProvince().enqueue(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_image:
                finish();
                break;
            case R.id.province_radio_linear:
                break;
        }
    }

    @Override
    public void onResponse(Call<ProvinceList> call, Response<ProvinceList> response) {
        List<ProvinceList.ResultBean> result = response.body().getResult();
        String[] titles = new String[result.size()];
        List<Fragment> list = new ArrayList<>();
        for (int i = 0; i < result.size(); i++) {
            String id = result.get(i).getProvinceCode();
            String name = result.get(i).getProvinceName();
            titles[i] = name;
            ProvinceRadioFragment provinceRadioFragment = ProvinceRadioFragment.newInstance(id);
            list.add(provinceRadioFragment);
        }
        DiscoverAdapter adapter = new DiscoverAdapter(getSupportFragmentManager(), list, this, titles);
        viewPager.setAdapter(adapter);
        tab.setupWithViewPager(viewPager);

    }

    @Override
    public void onFailure(Call<ProvinceList> call, Throwable t) {

    }
}
